package com.dkt.android.mvp.presenter

import com.dkt.android.mvp.model.LoginInfo
import com.dtb.utils.base.Regular
import com.dtb.utils.commons.utils.parity
import com.dtb.utils.commons.utils.parityEmpty
import com.dtb.utils.commons.utils.parityEquals
import com.dtb.utils.commons.utils.paritySize


/**
 * @author: dtb
 * @email: wendaoyuqin1@gmail.com
 * @createTime: 19-3-29下午3:25
 */

object UserInfoParity {

    const val PSW_MIN = 6
    const val PSW_MAX = 12

    fun parityUserInfo(info: LoginInfo, err: (str: String) -> Unit): Boolean {
        val n = info.account
        val p = info.pwd
        return parityUserName(n, err) && p.parityEmpty {
            err.invoke("密码不能为空")
        } && p.paritySize(PSW_MIN, PSW_MAX) {
            err.invoke("请输入密码长度在${PSW_MIN}-${PSW_MAX}位之间")
        }
    }

    fun parityUserName(n: String?, err: (str: String) -> Unit): Boolean {
        return n.parityEmpty {
            err.invoke("用户名不能为空")
        } && n.paritySize(1, PSW_MAX) {
            err.invoke("用户名长度在不能超过${PSW_MAX}位")
        }
    }

    fun parityNameAndSms(n: String?, sms: String?, err: (str: String) -> Unit): Boolean =
        parityUserName(n, err) && paritySms(sms, err)


    fun paritySms(n: String?, err: (str: String) -> Unit): Boolean =
        n.parityEmpty {
            err.invoke("验证码不能为空")
        } && n.paritySize(5, 5) {
            err.invoke("验证码长度错误")
        }


    fun parityPsw(n1: String?, n2: String?, err: (str: String) -> Unit): Boolean =
        n1.parityEmpty {
            err.invoke("密码不能为空")
        } && n2.parityEmpty {
            err.invoke("密码不能为空")
        } && n1.paritySize(PSW_MIN, PSW_MAX) {
            err.invoke("密码长度请设置为${PSW_MIN}-${PSW_MAX}位")
        } && n1.parityEquals(n2) {
            err.invoke("两次输入的密码不一致")
        }

    fun parityModifyPsw(old: String?, n1: String?, n2: String?, err: (str: String) -> Unit): Boolean =
        old.paritySize(PSW_MIN, PSW_MAX) {
            err.invoke("请输入密码长度在${PSW_MIN}-${PSW_MAX}位之间")
        } && parityPsw(n1, n2) {
            err.invoke(it)
        }


    var loginInfo: LoginInfo? = null


    fun initLoginInfo(phone: String, code: String, sms: String) {
        loginInfo = LoginInfo()
        loginInfo?.account = phone
        loginInfo?.authCode = code
        loginInfo?.smsCode = sms
    }
}